<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="物业名" prop="name">
        <el-input v-model="dataForm.name" placeholder="物业名"></el-input>
      </el-form-item>
      <el-form-item label="物业证书" prop="cridential">
        <el-input v-model="dataForm.cridential" placeholder="物业证书"></el-input>
      </el-form-item>
      <el-form-item label="物业电话" prop="phone">
        <el-input v-model.number="dataForm.phone" placeholder="物业电话"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        managerId: 0,
        name: "",
        cridential: "",
        phone: "",
        state: "",
        createtime: "",
        updatetime: ""
      },
      dataRule: {
        name: [{ required: true, message: "物业名不能为空", trigger: "blur" }],
        cridential: [
          { required: true, message: "物业证书不能为空", trigger: "blur" }
        ],
        phone: [
          {
            validator: (rule, value, callback) => {
              if (!value) {
                return callback(new Error("手机不能为空"));
              }
              setTimeout(() => {
                if (!Number.isInteger(value)) {
                  callback(new Error("请输入数字值"));
                } else {
                  if (!/^1[0-9]{10}$/.test(value)) {
                    callback(new Error("输入正确手机号"));
                  } else {
                    callback();
                  }
                }
              }, 1000);
            },
            trigger: "blur"
          }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.managerId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.managerId) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/manager/info/${this.dataForm.managerId}`
            ),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.name = data.manager.name;
              this.dataForm.cridential = data.manager.cridential;
              this.dataForm.phone = data.manager.phone;
              this.dataForm.state = data.manager.state;
              this.dataForm.createtime = data.manager.createtime;
              this.dataForm.updatetime = data.manager.updatetime;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/manager/manager/${!this.dataForm.managerId ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              managerId: this.dataForm.managerId || undefined,
              name: this.dataForm.name,
              cridential: this.dataForm.cridential,
              phone: this.dataForm.phone,
              state: this.dataForm.state,
              createtime: this.dataForm.createtime,
              updatetime: this.dataForm.updatetime
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    }
  }
};
</script>
